home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectPlay / Chat / frmChat.frm next >
Text File  |  2001-10-08  |  11KB  |  288 lines

  1. VERSION 5.00
  2. Begin VB.Form frmChat 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "vbDirectPlay Chat"
  5.    ClientHeight    =   5085
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   7695
  9.    Icon            =   "frmChat.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   5085
  14.    ScaleWidth      =   7695
  15.    StartUpPosition =   3  'Windows Default
  16.    Begin VB.CommandButton cmdWhisper 
  17.       Caption         =   "Whisper"
  18.       Height          =   255
  19.       Left            =   5820
  20.       TabIndex        =   3
  21.       Top             =   4740
  22.       Width           =   1695
  23.    End
  24.    Begin VB.TextBox txtSend 
  25.       Height          =   285
  26.       Left            =   60
  27.       TabIndex        =   0
  28.       Top             =   4740
  29.       Width           =   5595
  30.    End
  31.    Begin VB.ListBox lstUsers 
  32.       Height          =   4545
  33.       Left            =   5760
  34.       TabIndex        =   2
  35.       Top             =   120
  36.       Width           =   1815
  37.    End
  38.    Begin VB.TextBox txtChat 
  39.       Height          =   4635
  40.       Left            =   60
  41.       Locked          =   -1  'True
  42.       MultiLine       =   -1  'True
  43.       ScrollBars      =   3  'Both
  44.       TabIndex        =   1
  45.       TabStop         =   0   'False
  46.       Top             =   60
  47.       Width           =   5595
  48.    End
  49. End
  50. Attribute VB_Name = "frmChat"
  51. Attribute VB_GlobalNameSpace = False
  52. Attribute VB_Creatable = False
  53. Attribute VB_PredeclaredId = True
  54. Attribute VB_Exposed = False
  55. Option Explicit
  56. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  57. '
  58. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  59. '
  60. '  File:       frmChat.frm
  61. '
  62. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  63. Implements DirectPlay8Event
  64.  
  65. Private Sub cmdWhisper_Click()
  66.     Dim lMsg As Long, lOffset As Long
  67.     Dim sChatMsg As String
  68.     Dim oBuf() As Byte
  69.     
  70.     If lstUsers.ListIndex < 0 Then
  71.         MsgBox "You must select a user in the list before you can whisper to that person.", vbOKOnly Or vbInformation, "Select someone"
  72.         Exit Sub
  73.     End If
  74.     
  75.     If lstUsers.ItemData(lstUsers.ListIndex) = 0 Then
  76.         MsgBox "Why are you whispering to yourself?", vbOKOnly Or vbInformation, "Select someone else"
  77.         Exit Sub
  78.     End If
  79.     
  80.     If txtSend.Text = vbNullString Then
  81.         MsgBox "What's the point of whispering if you have nothing to say..", vbOKOnly Or vbInformation, "Enter text"
  82.         Exit Sub
  83.     End If
  84.         
  85.     'Send this message to the person you are whispering to
  86.     lMsg = MsgWhisper
  87.     lOffset = NewBuffer(oBuf)
  88.     AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  89.     sChatMsg = txtSend.Text
  90.     AddStringToBuffer oBuf, sChatMsg, lOffset
  91.     txtSend.Text = vbNullString
  92.     dpp.SendTo lstUsers.ItemData(lstUsers.ListIndex), oBuf, 0, DPNSEND_NOLOOPBACK
  93.     UpdateChat "**<" & gsUserName & ">** " & sChatMsg
  94.     
  95. End Sub
  96.  
  97. Private Sub Form_Load()
  98.  
  99.     'Oh good, we want to play a multiplayer game.
  100.     'First lets get the dplay connection started
  101.     
  102.     'Here we will init our DPlay objects
  103.     InitDPlay
  104.     'Now we can create a new Connection Form (which will also be our message pump)
  105.     Set DPlayEventsForm = New DPlayConnect
  106.     'Start the connection form (it will either create or join a session)
  107.     If Not DPlayEventsForm.StartConnectWizard(dx, dpp, AppGuid, 20, Me) Then
  108.         Cleanup
  109.         End
  110.     Else 'We did choose to play a game
  111.         gsUserName = DPlayEventsForm.UserName
  112.         If DPlayEventsForm.IsHost Then
  113.             Me.Caption = Me.Caption & " (HOST)"
  114.         End If
  115.     End If
  116. End Sub
  117.  
  118. Private Sub Form_Unload(Cancel As Integer)
  119.     Me.Hide
  120.     DPlayEventsForm.DoSleep 50
  121.     Cleanup
  122. End Sub
  123.  
  124. Private Sub UpdateChat(ByVal sString As String)
  125.     'Update the chat window first
  126.     txtChat.Text = txtChat.Text & sString & vbCrLf
  127.     'Now limit the text in the window to be 16k
  128.     If Len(txtChat.Text) > 16384 Then
  129.         txtChat.Text = Right$(txtChat.Text, 16384)
  130.     End If
  131.     'Autoscroll the text
  132.     txtChat.SelStart = Len(txtChat.Text)
  133. End Sub
  134.  
  135. Private Sub txtSend_KeyPress(KeyAscii As Integer)
  136.     Dim lMsg As Long, lOffset As Long
  137.     Dim sChatMsg As String
  138.     Dim oBuf() As Byte
  139.     
  140.     If KeyAscii = vbKeyReturn Then
  141.         If txtSend.Text <> vbNullString Then 'Make sure they are trying to send something
  142.             'Send this message to everyone
  143.             lMsg = MsgChat
  144.             lOffset = NewBuffer(oBuf)
  145.             AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  146.             sChatMsg = txtSend.Text
  147.             AddStringToBuffer oBuf, sChatMsg, lOffset
  148.             txtSend.Text = vbNullString
  149.             KeyAscii = 0
  150.             dpp.SendTo DPNID_ALL_PLAYERS_GROUP, oBuf, 0, DPNSEND_NOLOOPBACK
  151.             UpdateChat "<" & gsUserName & ">" & sChatMsg
  152.         End If 'We won't set KeyAscii to 0 here, because if they are trying to
  153.                'send blank data, we don't care about the ding for hitting enter on
  154.                'an empty line
  155.     End If
  156. End Sub
  157.  
  158. Private Function GetName(ByVal lID As Long) As String
  159.     Dim lCount As Long
  160.     
  161.     GetName = vbNullString
  162.     For lCount = 0 To lstUsers.ListCount - 1
  163.         If lstUsers.ItemData(lCount) = lID Then 'This is the player
  164.             GetName = lstUsers.List(lCount)
  165.             Exit For
  166.         End If
  167.     Next
  168. End Function
  169.  
  170. Private Sub DirectPlay8Event_AddRemovePlayerGroup(ByVal lMsgID As Long, ByVal lPlayerID As Long, ByVal lGroupID As Long, fRejectMsg As Boolean)
  171.     'VB requires that we must implement *every* member of this interface
  172. End Sub
  173.  
  174. Private Sub DirectPlay8Event_AppDesc(fRejectMsg As Boolean)
  175.     'VB requires that we must implement *every* member of this interface
  176. End Sub
  177.  
  178. Private Sub DirectPlay8Event_AsyncOpComplete(dpnotify As DxVBLibA.DPNMSG_ASYNC_OP_COMPLETE, fRejectMsg As Boolean)
  179.     'VB requires that we must implement *every* member of this interface
  180. End Sub
  181.  
  182. Private Sub DirectPlay8Event_ConnectComplete(dpnotify As DxVBLibA.DPNMSG_CONNECT_COMPLETE, fRejectMsg As Boolean)
  183.     If dpnotify.hResultCode <> 0 Then
  184.         'For some reason we could not connect.  All available slots must be closed.
  185.         MsgBox "Connect Failed.  Error: 0x" & CStr(Hex$(dpnotify.hResultCode)) & "  - This sample will now close.", vbOKOnly Or vbCritical, "Closing"
  186.         DPlayEventsForm.CloseForm Me
  187.     End If
  188. End Sub
  189.  
  190. Private Sub DirectPlay8Event_CreateGroup(ByVal lGroupID As Long, ByVal lOwnerID As Long, fRejectMsg As Boolean)
  191.     'VB requires that we must implement *every* member of this interface
  192. End Sub
  193.  
  194. Private Sub DirectPlay8Event_CreatePlayer(ByVal lPlayerID As Long, fRejectMsg As Boolean)
  195.     Dim dpPeer As DPN_PLAYER_INFO
  196.     dpPeer = dpp.GetPeerInfo(lPlayerID)
  197.         
  198.     'Add this person to chat (even if it's me)
  199.     lstUsers.AddItem dpPeer.Name
  200.     If (dpPeer.lPlayerFlags And DPNPLAYER_LOCAL) <> DPNPLAYER_LOCAL Then 'this isn't me, someone just joined
  201.         UpdateChat "- " & dpPeer.Name & " is chatting"
  202.         'If it's not me, include an ItemData
  203.         lstUsers.ItemData(lstUsers.ListCount - 1) = lPlayerID
  204.     End If
  205. End Sub
  206.  
  207. Private Sub DirectPlay8Event_DestroyGroup(ByVal lGroupID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  208.     'VB requires that we must implement *every* member of this interface
  209. End Sub
  210.  
  211. Private Sub DirectPlay8Event_DestroyPlayer(ByVal lPlayerID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  212.     Dim lCount As Long
  213.     
  214.     'We only care when someone leaves.  When they join we will receive a 'MSGJoin'
  215.     'Remove this player from our list
  216.     For lCount = 0 To lstUsers.ListCount - 1
  217.         If lstUsers.ItemData(lCount) = lPlayerID Then 'This is the player
  218.             UpdateChat "-- " & lstUsers.List(lCount) & " is no longer chatting."
  219.             lstUsers.RemoveItem lCount
  220.             Exit For
  221.         End If
  222.     Next
  223. End Sub
  224.  
  225. Private Sub DirectPlay8Event_EnumHostsQuery(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_QUERY, fRejectMsg As Boolean)
  226.     'VB requires that we must implement *every* member of this interface
  227. End Sub
  228.  
  229. Private Sub DirectPlay8Event_EnumHostsResponse(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_RESPONSE, fRejectMsg As Boolean)
  230.     'VB requires that we must implement *every* member of this interface
  231. End Sub
  232.  
  233. Private Sub DirectPlay8Event_HostMigrate(ByVal lNewHostID As Long, fRejectMsg As Boolean)
  234.     Dim dpPeer As DPN_PLAYER_INFO
  235.     dpPeer = dpp.GetPeerInfo(lNewHostID)
  236.     If (dpPeer.lPlayerFlags And DPNPLAYER_LOCAL) = DPNPLAYER_LOCAL Then 'I am the new host
  237.         Me.Caption = Me.Caption & " (HOST)"
  238.     End If
  239. End Sub
  240.  
  241. Private Sub DirectPlay8Event_IndicateConnect(dpnotify As DxVBLibA.DPNMSG_INDICATE_CONNECT, fRejectMsg As Boolean)
  242.     'VB requires that we must implement *every* member of this interface
  243. End Sub
  244.  
  245. Private Sub DirectPlay8Event_IndicatedConnectAborted(fRejectMsg As Boolean)
  246.     'VB requires that we must implement *every* member of this interface
  247. End Sub
  248.  
  249. Private Sub DirectPlay8Event_InfoNotify(ByVal lMsgID As Long, ByVal lNotifyID As Long, fRejectMsg As Boolean)
  250.     'VB requires that we must implement *every* member of this interface
  251. End Sub
  252.  
  253. Private Sub DirectPlay8Event_Receive(dpnotify As DxVBLibA.DPNMSG_RECEIVE, fRejectMsg As Boolean)
  254.     'process what msgs we receive.
  255.     Dim lMsg As Long, lOffset As Long
  256.     Dim dpPeer As DPN_PLAYER_INFO, sName As String
  257.     Dim sChat As String
  258.     
  259.     With dpnotify
  260.     GetDataFromBuffer .ReceivedData, lMsg, LenB(lMsg), lOffset
  261.     Select Case lMsg
  262.     Case MsgChat
  263.         sName = GetName(.idSender)
  264.         sChat = GetStringFromBuffer(.ReceivedData, lOffset)
  265.         UpdateChat "<" & sName & "> " & sChat
  266.     Case MsgWhisper
  267.         sName = GetName(.idSender)
  268.         sChat = GetStringFromBuffer(.ReceivedData, lOffset)
  269.         UpdateChat "**<" & sName & ">** " & sChat
  270.     End Select
  271.     End With
  272.     
  273. End Sub
  274.  
  275. Private Sub DirectPlay8Event_SendComplete(dpnotify As DxVBLibA.DPNMSG_SEND_COMPLETE, fRejectMsg As Boolean)
  276.     'VB requires that we must implement *every* member of this interface
  277. End Sub
  278.  
  279. Private Sub DirectPlay8Event_TerminateSession(dpnotify As DxVBLibA.DPNMSG_TERMINATE_SESSION, fRejectMsg As Boolean)
  280.     If dpnotify.hResultCode = DPNERR_HOSTTERMINATEDSESSION Then
  281.         MsgBox "The host has terminated this session.  This sample will now exit.", vbOKOnly Or vbInformation, "Exiting"
  282.     Else
  283.         MsgBox "This session has been lost.  This sample will now exit.", vbOKOnly Or vbInformation, "Exiting"
  284.     End If
  285.     DPlayEventsForm.CloseForm Me
  286. End Sub
  287.  
  288.